package com.imay.live.adapter.holder;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.activity.ProfileActivity;
import com.imay.live.activity.discovery.FollowControl;
import com.imay.live.adapter.DynamicsSuggestAdapter;
import com.imay.live.fragment.dynamics.OnTouchHeartListener;
import com.imay.live.model.UserModel;
import com.imay.live.view.AutoAdjustRecylerView;
import com.imay.live.R;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 推荐列表
 * Created by chan on 2017/5/2.
 */

public class SuggestViewHolder {
    private AutoAdjustRecylerView mRecyclerView;
    FollowControl followControl;

    private DynamicsSuggestAdapter mAdapter;

    public SuggestViewHolder() {
        followControl = new FollowControl();
    }


    public void convert(BaseViewHolder helper, final List list, final OnTouchHeartListener listener) {
        if(mAdapter == null){
            mAdapter = new DynamicsSuggestAdapter();
        }
        if (mRecyclerView == null) {
            mRecyclerView = helper.getView(R.id.rcv_dynamics);
            mRecyclerView.setNestedScrollingEnabled(false);
            LinearLayoutManager labelsLayoutManager = new LinearLayoutManager(helper.getConvertView().getContext());
            labelsLayoutManager.setOrientation(LinearLayout.HORIZONTAL);
            mRecyclerView.setLayoutManager(labelsLayoutManager);
            mRecyclerView.setPxPerMillsec(1.0f);
            mRecyclerView.setAdapter(mAdapter);

            mRecyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(helper.getConvertView().getContext())
                    .color(Color.TRANSPARENT)
                    .sizeResId(R.dimen.size_3dp)
                    .build());

            /*mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        //listener.suggestTouch(false);
                    }
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    //listener.suggestTouch(true);
                }
            });*/
        }


        ArrayList<UserModel> userModels = (ArrayList<UserModel>) list;
        mAdapter.setNewData(userModels);
        mAdapter.notifyDataSetChanged();

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Object item = list.get(position);
                switch (view.getId()) {
                    case R.id.tv_item_dynamics_suggest_follow:
                        if (item instanceof UserModel) {
                            UserModel userModel = (UserModel) item;
                            followControl.followChanged(view.getContext(), userModel, false);
                            view.setSelected(true);
                            view.setClickable(false);
                            ((TextView) view).setText(view.getContext().getString(R.string.has_followed));
                        }
                        break;
                    case R.id.civ_item_dynamics_suggest_img:
                        if (item instanceof UserModel) {
                            UserModel userModel = (UserModel) item;
                            ProfileActivity.start(view.getContext(), userModel.getUid());
                        }
                        break;
                }
            }
        });
    }
}
